import requests
from lxml import etree
if __name__ == "__main__":
    #页面原码数据
    url = 'https://wf.58.com/ershoufang/'
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67'
    }
    page_text = requests.get(url=url,headers=headers).text
    tree = etree.HTML(page_text)
    li_tree = tree.xpath('//section[@class="list"]/div')
    fp = open('58.txt','w',encoding='utf-8')
    for li in li_tree:
        title = li.xpath('./a/div[2]//div/h3/text()')[0]
        price = li.xpath('./a/div[2]/div[2]/p[1]/span[1]/text()')[0]
        pprice= li.xpath('./a/div[2]/div[2]/p[2]/text()')[0]
        print(title)
        fp.write(title+'   '+price + '万' + '    '+pprice+'\n')
    
